package com.sld.concrete.vehicle.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.sld.concrete.algorithm.domain.RecDevCalculate;
import com.sld.concrete.algorithm.mapper.RecDevCalculateMapper;
import com.sld.concrete.device.domain.RecDevWorkStandard;
import com.sld.concrete.device.mapper.RecDevWorkStandardMapper;
import com.sld.concrete.register.domain.InfoConcreteRegister;
import com.sld.concrete.register.mapper.InfoConcreteRegisterMapper;
import com.sld.concrete.vehicle.domain.RecDevWorkSequence;
import com.sld.concrete.vehicle.mapper.RecDevWorkSequenceMapper;
import com.sld.concrete.vehicle.service.IRecDevWorkSequenceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 工作序列Service业务层处理
 * 
 * @author topviewers
 * @date 2022-04-23
 */
@Service
public class RecDevWorkSequenceServiceImpl implements IRecDevWorkSequenceService 
{
    @Resource
    private RecDevWorkSequenceMapper recDevWorkSequenceMapper;

    @Resource
    InfoConcreteRegisterMapper registerMapper;

    @Resource
    RecDevWorkStandardMapper workStandardMapper;

    @Resource
    RecDevCalculateMapper calculateMapper;


    /**
     * 查询工作序列
     * 
     * @param id 工作序列主键
     * @return 工作序列
     */
    @Override
    public RecDevWorkSequence selectRecDevWorkSequenceById(Long id)
    {
        return recDevWorkSequenceMapper.selectRecDevWorkSequenceById(id);
    }

    /**
     * 查询工作序列列表
     * 
     * @param recDevWorkSequence 工作序列
     * @return 工作序列
     */
    @Override
    public List<RecDevWorkSequence> selectRecDevWorkSequenceList(RecDevWorkSequence recDevWorkSequence)
    {
        return recDevWorkSequenceMapper.selectRecDevWorkSequenceList(recDevWorkSequence);
    }

    /**
     * 新增工作序列
     * 
     * @param recDevWorkSequence 工作序列
     * @return 结果
     */
    @Override
    public int insertRecDevWorkSequence(RecDevWorkSequence recDevWorkSequence)
    {
        return recDevWorkSequenceMapper.insertRecDevWorkSequence(recDevWorkSequence);
    }

    /**
     * 修改工作序列
     * 
     * @param recDevWorkSequence 工作序列
     * @return 结果
     */
    @Override
    public int updateRecDevWorkSequence(RecDevWorkSequence recDevWorkSequence)
    {
        return recDevWorkSequenceMapper.updateRecDevWorkSequence(recDevWorkSequence);
    }

    /**
     * 批量删除工作序列
     * 
     * @param ids 需要删除的工作序列主键
     * @return 结果
     */
    @Override
    public int deleteRecDevWorkSequenceByIds(String ids)
    {
        return recDevWorkSequenceMapper.deleteRecDevWorkSequenceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除工作序列信息
     * 
     * @param id 工作序列主键
     * @return 结果
     */
    @Override
    public int deleteRecDevWorkSequenceById(Long id)
    {
        return recDevWorkSequenceMapper.deleteRecDevWorkSequenceById(id);
    }

    /**
     * baseInfo: {
     *           plateNo: '鲁B1C2N9',
     *           workDate: '2022-04-18',
     *           workSequence: '01',
     *           duration: '00:34:21',
     *           startTime: '2022-04-18 12:12:12',
     *           endTime: '2022-04-18 12:12:12',
     *           insertTime: '2022-04-18 12:12:12',
     *           companyName: '山东精砼',
     *           mixingStation: '商混站',
     *           machineUnit: '4号位',
     *           slumpStart: '50',
     *           slumpEnd: '40',
     *           concreteType: 'C11',
     *           concreteVolume: '5'
     *    },
     *     detectInfo: {
     *         calibration: {
     *             pressure:'10.101',
     *             speed:'10.101',
     *             temperature:'10.101',
     *             humidity:'10.101'
     *         },
     *         calculates: [
     *             {
     *                 id: 5,
     *                 insertTime:'2022-04-18 12:12:12',
     *                 calcResult:10,
     *                 pressure: '11',
     *                 rotateSpeed:'11',
     *                 humidity:'11',
     *                 temperature:'11',
     *                 groupType:'A',
     *             }
     *         ]
     *     },
     */
    @Override
    public Map<String, Object> queryWorkDetail(String seqId) {
        long workId = Long.parseLong(seqId);
        JSONObject baseInfo = new JSONObject();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        //设置时区，跳过此步骤会默认设置为"GMT+08:00" 得到的结果会多出来8个小时
        format.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));

        RecDevWorkSequence sequence = recDevWorkSequenceMapper.selectRecDevWorkSequenceById(workId);
        Long registerId = sequence.getRegisterId();
        InfoConcreteRegister register = registerMapper.selectInfoConcreteRegisterByRegisterId(registerId);
        Date endTime = sequence.getEndTime();
        if (endTime == null) {
            endTime = new Date();
        }
        long between = endTime.getTime() - sequence.getStartTime().getTime();
        String duration = format.format(between);

        baseInfo.put("plateNo", sequence.getPlateNo());
        baseInfo.put("workDate", DateUtils.parseDateToStr("yyyy-MM-dd", sequence.getWorkDate()));
        baseInfo.put("workSequence", sequence.getWorkSequence());
        baseInfo.put("duration", duration);
        baseInfo.put("startTime", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", sequence.getStartTime()));
        baseInfo.put("endTime", sequence.getEndTime() == null? "进行中" : DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",endTime));
        baseInfo.put("insertTime", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", sequence.getInsertTime()));

        if (register != null) {
            baseInfo.put("companyName", register.getCompanyName());
            baseInfo.put("mixingStation", register.getMixingStation());
            baseInfo.put("machineUnit", register.getMachineUnit());
            baseInfo.put("slumpStart", register.getSlumpStart());
            baseInfo.put("slumpEnd", register.getSlumpEnd());
            baseInfo.put("concreteType", register.getConcreteType());
            baseInfo.put("concreteVolume", register.getConcreteVolume());
        }

        JSONObject detectInfo = new JSONObject();
        RecDevWorkStandard calibration = workStandardMapper.selectRecDevWorkStandardByWorkSeqId(workId);
        RecDevCalculate condition = new RecDevCalculate();
        condition.setWorkId(workId);
        List<RecDevCalculate> calculates = calculateMapper.selectRecDevCalculateList(condition);
        detectInfo.put("calibration", calibration);
        detectInfo.put("calculates", calculates);

        Map<String, Object> res = new HashMap<>();
        res.put("baseInfo", baseInfo);
        res.put("detectInfo", detectInfo);
        return res;
    }
}
